﻿CloudDisk.Admin.GroupsPanel = (function(cfg){
    var el = $('#groupsPanel');
	
	var groupItemTpl = $('#groupItemTpl');
	
	var memberItemTpl = $('#simpleMemberItemTpl');
	
	var selectedGroup = null;
	
	
	
	var groups = [];
	
	var members = [];
	
    var initialized = false;
	
    return {
	    init: function(){
		    var that = this;
		    //新建分组
			el.find('button[action=addGroup]').click(function(){
				CloudDisk.Admin.AddGroupWindow.open(function(o){		
					CloudDisk.Admin.SessionMgr.addGroup(o, function(){			    
						el.find('.adm-group-list').append(that.createGroupItem(o));
					});		
				});
			});
			
		    //添加组员
			el.find('button[action=addMember]').click(function(){
				CloudDisk.Admin.AddMemberToGroupWindow.open({
				    groupId: selectedGroup.group_id,
				    callback: function(){
                        CloudDisk.Admin.SessionMgr.queryUserList(function(data){			    
							members = data.result;
							that.renderMemberItems();
						});					
					}
				});
			});			
			
			//重命名分组
			el.find('button[action=renameGroup]').click(function(){
				el.find('.adm-group-detail-title').hide();
				el.find('.adm-group-rename-wrap input').val(selectedGroup.group_name);
				el.find('.adm-group-rename-wrap').show();
			});
			
			//删除分组
			el.find('button[action=deleteGroup]').click(function(){				
				var dialog = new CloudDisk.MessageDialog('您确认要删除该分组吗?', '删除分组');
				dialog.appendCommand('OK',  function(){
					CloudDisk.Admin.SessionMgr.deleteGroup(selectedGroup, function(){			    
			            that.activate();
					});
				});
				dialog.appendCommand('取消',  function(){
				});
				dialog.show();
			});
			
			//提交重命名分组
			el.find('button[action=doRenameGroup]').click(function(){
			    selectedGroup.group_name = el.find('.adm-group-rename-wrap input').val();
				CloudDisk.Admin.SessionMgr.updateGroup(selectedGroup, function(){			    
					
				});	
				el.find('.adm-group-rename-wrap').hide();
				el.find('.adm-group-detail-title').html(selectedGroup.group_name).show();		
			});
			
			//取消重命名分组
			el.find('button[action=cancelRenameGroup]').click(function(){
				el.find('.adm-group-rename-wrap').hide();
				el.find('.adm-group-detail-title').html(selectedGroup.group_name).show();		
			});
		},
	
	    render: function(){
		    this.renderGroupItems();
		},
		
		renderGroupItems: function(){
		    var ct = el.find('.adm-group-list');
		    ct.empty();
			$.each(_groups, function(n, v){
				ct.append($('#groupItemTpl').render(v));
			});
		},
		
		renderMemberItems: function(){
	        var body = el.find('.adm-member-list');			
			body.empty();
			for(var i = 0; i < members.length; ++i){
			    if(members[i].group_id == selectedGroup.group_id){
				    body.append(this.createMemberItem(members[i]));
				}
			}
		},
		
		createGroupItem: function(data){
			var item = $('<div class="adm-group-item"></div>');
			var that = this;
			item.html(groupItemTpl.render(data));
			item.attr('group-id', data.group_id);
			
			item.on('click', function(e){
				var tar = e.target;
				var itemId = $(this).attr('group-id');
				
				el.find('.adm-group-list .selected').removeClass('selected');
				$(this).addClass('selected');
				for(var i = 0; i < groups.length; ++i){
					if(groups[i].group_id + '' == itemId){
						selectedGroup = groups[i];
						that.updateGroupDetail(selectedGroup);
						break;
					}
				}
			});

			return item;
		},
		
		createMemberItem: function(data){
			var item = $('<div class="adm-member-item"></div>');
			var that = this;
			item.html(memberItemTpl.render(data));
			item.attr('member-id', data.user_id);
			
			item.on('click', function(e){
				var tar = e.target;
				var itemId = $(this).attr('member-id');
				if(tar.getAttribute('action') == 'moveMember'){
					for(var i = 0; i < members.length; ++i){
						if(members[i].user_id + '' == itemId){
							members[i].group_id = '';					
							CloudDisk.Admin.SessionMgr.updateUser(members[i], function(){			    
                                that.renderMemberItems();
							});
							break;
						}
					}
				}
			});
			return item;
		},
		
		updateGroupDetail: function(){
		    el.find('.adm-group-detail-title').html(selectedGroup.group_name);
			el.find('.adm-group-rename-wrap input').val(selectedGroup.group_name);
			
			var that = this;
            if(members.length < 1){
				CloudDisk.Admin.SessionMgr.queryUserList(function(data){			    
					members = data.result;
					that.renderMemberItems();
				});
			} else {
			    that.renderMemberItems();
			}
		},
		
		
		activate: function(){
		    var that = this;
		    el.show();
			if(!initialized){
			    that.init();
				initialized = true;
			}
				
			CloudDisk.Admin.SessionMgr.queryGroupList(function(data){			    
				groups = data.result;
				var ct = el.find('.adm-group-list');

				ct.empty();
				for(var i = 0; i < groups.length; ++i){
				    ct.append(that.createGroupItem(groups[i]));
				}	
				
				if(groups.length > 0){
				    ct.find('.adm-group-item').first().addClass('selected');
					selectedGroup = groups[0];
					
					that.updateGroupDetail();
				}
			});	

			

		}
	};
})();

CloudDisk.Admin.AddGroupWindow = (function(){
    var tpl = [
	    '<div class="" style="display: block; width: auto; min-height: 0px; height: 431px;padding:20px;">',
            '<div class="">分组名：</div>',
			'<div class="">',
			    '<input name="groupName" type="text" size="20">',
			'</div>',		
			'<div class="">描述：</div>',
			'<div class="">',
			    '<textarea name="groupDesc" cols="20" rows="3"></textarea>',
			'</div>',	
            '<div class="add_member_ops">',
			    '<button action="ok">确定</button>',
				'<button action="cancel">取消</button>',
			'</div>',
		'</div>'].join('');
	
	return new CloudDisk.Window({
	    title: '新建分组',
		content: tpl,
		width: 280,
		height:200,
		open: function(callback){
		    this.callback = callback;
	        CloudDisk.Util.showMask();
			this.el.show();
			this.position();	
			
			//this.el.find('.selectpicker').selectpicker();		
		},
		clickHandler: function(e, o){
		    var tar = e.target;
			if(tar.getAttribute('action') == 'ok'){
			    var groupName = o.el.find('input[name=groupName]').val();
				var groupDesc = o.el.find('textarea[name=groupDesc]').val();
			    o.callback({
					group_name: groupName,
					group_desc: groupDesc
				});
			    o.close();
			} else if(tar.getAttribute('action') == 'cancel'){
			    o.close();
			} 
		}
	});
})();

CloudDisk.Admin.AddMemberToGroupWindow = (function(){
    var tpl = [
	    '<div class="selection-search gray_bg c_f">',
            '<div class="search-bar">',
			    '<input type="text" placeholder="搜索">',
			    '<a class="search-bar-btn" href="javascript:void(0);"></a>',
		    '</div>',
		'</div>',
		'<div class="selection-members scrollbar"></div>'
	].join('');
	
	var memberCardTpl = $.templates([
		'<div class="photo">',
			'<img alt="头像" src="images/role.jpg">',
		'</div>',
		'<div class="member-info-wrp">',
			'<div class="member_name" title="{{:user_name}}">{{:user_name}}</div>',
			'<div class="member_title">',
				'<button class="btn blue_btn" action="moveMember">添加</button>',
			'</div>',
		'</div>',
	].join(''));
	
	var members = [];
	
	var selectedGroupId;
	
	return new CloudDisk.Window({
	    title: '添加组员',
		content: tpl,
		width: 800,

		open: function(cfg){
		    selectedGroupId = cfg.groupId;
	        CloudDisk.Util.showMask();
			this.callback = cfg.callback;
			this.el.show();
			this.position();	
					
			var ct = this.el.find('.selection-members');
			var that =this;
			ct.empty();
			ct.html('加载中...');
			CloudDisk.Admin.SessionMgr.queryUserList(function(data){	
                ct.empty();		    
                members = data.result;
				
				var count = 0;
				for(var i = 0; i < members.length; ++i){
				    if(!members[i].group_id){ //only show member not grouped
				        ct.append(that.createMemberCard(members[i]));
						count++;
					}
				}
				if(count == 0){
				    ct.html('暂无未分组的成员');
				}
			});
					
		},
		
		close: function () {
		    this.callback();
			this.el.hide();
			CloudDisk.Util.hideMask();
		},
			
		createMemberCard: function(data){
			var card = $('<div class="member-card"></div>');
			var that = this;
			card.html(memberCardTpl.render(data));
			card.attr('member-id', data.user_id);
			
			card.on('click', function(e){
				var tar = e.target;
				var memberId = $(this).attr('member-id');
				if(tar.getAttribute('action') == 'moveMember'){
					$(tar).removeAttr('action').addClass('disabled').html('添加中');
					for(var i = 0; i < members.length; ++i){
						if(members[i].user_id + '' == memberId){
							members[i].group_id = selectedGroupId;					
							CloudDisk.Admin.SessionMgr.updateUser(members[i], function(){			    
                                $(tar).html('已添加');
							});
							break;
						}
					}
				}
			});
			return card;
		},
		
		clickHandler: function(e, o){
		    var tar = e.target;
			if(tar.getAttribute('action') == 'addMember'){
			    o.callback({
					email: 'test123@qq.com',
					memberType: 2,
					groupId: ''
				});
			    o.close();
			}
		}
	});
})();